package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.FeedbackTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-10-23 22:25:03
 */
@Component
public class FeedbackTableSpecifications {

    public static Specification<FeedbackTable> queryList(FeedbackTable feedbackTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (feedbackTable.getFeedbackId() != null) {
                predicates.add(builder.like(root.get("feedbackId" ).as(String.class), "%" + feedbackTable.getFeedbackId() + "%" ));
            }
            if (feedbackTable.getFeedbackType() != null) {
                predicates.add(builder.like(root.get("feedbackType" ).as(String.class), "%" + feedbackTable.getFeedbackType() + "%" ));
            }
            if (feedbackTable.getFeedbackPersonName() != null) {
                predicates.add(builder.like(root.get("feedbackPersonName" ).as(String.class), "%" + feedbackTable.getFeedbackPersonName() + "%" ));
            }
            if (feedbackTable.getContactPhone() != null) {
                predicates.add(builder.like(root.get("contactPhone" ).as(String.class), "%" + feedbackTable.getContactPhone() + "%" ));
            }
            if (feedbackTable.getFeedbackTime() != null) {
                predicates.add(builder.equal(root.get("feedbackTime" ).as(Date.class), feedbackTable.getFeedbackTime()));
            }
            if (feedbackTable.getFeedbackContent() != null) {
                predicates.add(builder.like(root.get("feedbackContent" ).as(String.class), "%" + feedbackTable.getFeedbackContent() + "%" ));
            }
            if (feedbackTable.getProcessingStatus() != null) {
                predicates.add(builder.like(root.get("processingStatus" ).as(String.class), "%" + feedbackTable.getProcessingStatus() + "%" ));
            }
            if (feedbackTable.getProcessingResult() != null) {
                predicates.add(builder.like(root.get("processingResult" ).as(String.class), "%" + feedbackTable.getProcessingResult() + "%" ));
            }
            if (feedbackTable.getProcessingTime() != null) {
                predicates.add(builder.equal(root.get("processingTime" ).as(Date.class), feedbackTable.getProcessingTime()));
            }
            if (feedbackTable.getProcessorName() != null) {
                predicates.add(builder.like(root.get("processorName" ).as(String.class), "%" + feedbackTable.getProcessorName() + "%" ));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
